//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ExponentialForwardCorrelation.h"
using namespace Cephei::QL::Models::Marketmodels::Correlations;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (Cephei::Core::IVector<Double>^ rateTimes, Microsoft::FSharp::Core::FSharpOption<Double>^ longTermCorr, Microsoft::FSharp::Core::FSharpOption<Double>^ beta, Microsoft::FSharp::Core::FSharpOption<Double>^ gamma, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ times) : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
    try
    {
#ifdef HANDLE
        _phExponentialForwardCorrelation = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        QuantLib::Real _longTermCorr = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (longTermCorr) ? (QuantLib::Real)ValueHelper::Convert (longTermCorr->Value) : 0.5); //4
        QuantLib::Real _beta = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (beta) ? (QuantLib::Real)ValueHelper::Convert (beta->Value) : 0.2); //4
        QuantLib::Real _gamma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gamma) ? (QuantLib::Real)ValueHelper::Convert (gamma->Value) : 1.0); //4
        CDoubleVector^ _NCtimes;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (times))
        {
            times->Value->Lock();
            INativeVector<Double>^ _NCItimes = times->Value->getFeature (NativeFeature::Value);
            _NCtimes = safe_cast<CDoubleVector^>(_NCItimes);
        }
        std::vector<QuantLib::Time>& _times = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (times) ? static_cast<std::vector<QuantLib::Time>&> (_NCtimes->GetReference ()) : std::vector<QuantLib::Time>()); //3
        _ppExponentialForwardCorrelation = new boost::shared_ptr<QuantLib::ExponentialForwardCorrelation> (new QuantLib::ExponentialForwardCorrelation ( _rateTimes,  _longTermCorr,  _beta,  _gamma,  _times ));
        SetPiecewiseConstantCorrelation (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (times != nullptr) times->Value->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (boost::shared_ptr<QuantLib::ExponentialForwardCorrelation>& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phExponentialForwardCorrelation = NULL;
#endif
	_ppExponentialForwardCorrelation = &childNative;
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
}
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (QuantLib::ExponentialForwardCorrelation& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phExponentialForwardCorrelation = NULL;
#endif
	_ppExponentialForwardCorrelation = new boost::shared_ptr<QuantLib::ExponentialForwardCorrelation> (&childNative);
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
    _ExponentialForwardCorrelationOwner = owner;
    _PiecewiseConstantCorrelationOwner = owner;
}

Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (CExponentialForwardCorrelation^ copy) : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phExponentialForwardCorrelation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExponentialForwardCorrelation = new boost::shared_ptr<QuantLib::ExponentialForwardCorrelation> (copy->GetShared());
        _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (PLATFORM::Type^ t) : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phExponentialForwardCorrelation = NULL;
#endif
	if (!t->IsSubclassOf(CExponentialForwardCorrelation::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (QuantLib::Handle<QuantLib::ExponentialForwardCorrelation>& childNative, Object^ owner)  : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
	_phExponentialForwardCorrelation = &childNative;
	_ppExponentialForwardCorrelation = &static_cast<boost::shared_ptr<QuantLib::ExponentialForwardCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
    _ExponentialForwardCorrelationOwner = owner;
}
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (QuantLib::Handle<QuantLib::ExponentialForwardCorrelation> childNative)  : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
	_phExponentialForwardCorrelation = &childNative;
	_ppExponentialForwardCorrelation = &static_cast<boost::shared_ptr<QuantLib::ExponentialForwardCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::CExponentialForwardCorrelation (QuantLib::ExponentialForwardCorrelation childNative)  : CPiecewiseConstantCorrelation(CExponentialForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phExponentialForwardCorrelation = NULL;
#endif
	_ppExponentialForwardCorrelation = new boost::shared_ptr<QuantLib::ExponentialForwardCorrelation> (new QuantLib::ExponentialForwardCorrelation (childNative));
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppExponentialForwardCorrelation));
}
#endif

Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::~CExponentialForwardCorrelation ()
{
    if (_ppExponentialForwardCorrelation != NULL)
    {
	    delete _ppExponentialForwardCorrelation;
        _ppExponentialForwardCorrelation = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::!CExponentialForwardCorrelation ()
{
    if (_ppExponentialForwardCorrelation != NULL)
    {
	    delete _ppExponentialForwardCorrelation;
    }
}
QuantLib::ExponentialForwardCorrelation& Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::GetReference ()
{
    if (_ppExponentialForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return **_ppExponentialForwardCorrelation;
}
boost::shared_ptr<QuantLib::ExponentialForwardCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::GetShared ()
{
    if (_ppExponentialForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return *_ppExponentialForwardCorrelation;
}
QuantLib::ExponentialForwardCorrelation* Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::GetPointer ()
{
    if (_ppExponentialForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return &**_ppExponentialForwardCorrelation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExponentialForwardCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::GetHandle ()
{
	if (_phExponentialForwardCorrelation == NULL)
	{
		_phExponentialForwardCorrelation = new Handle<QuantLib::ExponentialForwardCorrelation> (*_ppExponentialForwardCorrelation);
	}
	return *_phExponentialForwardCorrelation;
}
#endif
bool Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::HasNative () 
{
	return (_ppExponentialForwardCorrelation != NULL);
}

Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::Correlations::get ()
{
    try
    {
    	std::vector<QuantLib::Matrix>& _rv = (std::vector<QuantLib::Matrix>&)(*_ppExponentialForwardCorrelation)->correlations ( );   
        CoVector<Cephei::QL::Math::IMatrix^>^ _nrv = REFNEW CoVector<Cephei::QL::Math::IMatrix^>(REFNEW CMatrixVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppExponentialForwardCorrelation)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppExponentialForwardCorrelation)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation::Times::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppExponentialForwardCorrelation)->times ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Correlations::IExponentialForwardCorrelation^ Cephei::QL::Models::Marketmodels::Correlations::CExponentialForwardCorrelation_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Microsoft::FSharp::Core::FSharpOption<Double>^ longTermCorr, Microsoft::FSharp::Core::FSharpOption<Double>^ beta, Microsoft::FSharp::Core::FSharpOption<Double>^ gamma, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ times)
{
    return REFNEW CExponentialForwardCorrelation ( rateTimes,  longTermCorr,  beta,  gamma,  times);
}
